package server;

import java.util.Calendar;
import java.util.concurrent.CopyOnWriteArrayList;

public class ServerManager {
	
	private static ServerManager instance = new ServerManager();

	private CopyOnWriteArrayList<Auction> runningAuctions;
	private CopyOnWriteArrayList<User> users;
	
	public static ServerManager getInstance() {
		return instance;
	}
	
	private ServerManager() {
		runningAuctions = new CopyOnWriteArrayList<Auction>();
		//runningAuctions.add(new Auction(new User("John", null, 0), "Auto", Calendar.getInstance()));
		//runningAuctions.add(new Auction(new User("Doe", null, 0), "Plane", Calendar.getInstance()));
		users = new CopyOnWriteArrayList<User>();
	}
	
	public CopyOnWriteArrayList<Auction> getRunningAuctions() {
		return runningAuctions;
	}
	public void setRunningAuctions(CopyOnWriteArrayList<Auction> runningAuctions) {
		this.runningAuctions = runningAuctions;
	}
	public CopyOnWriteArrayList<User> getUsers() {
		return users;
	}
	public void setUsers(CopyOnWriteArrayList<User> users) {
		this.users = users;
	}
	
	public synchronized Auction getAuction(int id) {
		Auction ret = null;
		Calendar now = Calendar.getInstance();
		for (Auction a : runningAuctions) {
			if (now.before(a.getDeadline())) {
				if (a.getId() == id) {
					ret = a;
					break;
				}
			}
		}
		
		return ret;
	}
	
	public synchronized CopyOnWriteArrayList<Auction> getOnlyRunningAuctions() {
		CopyOnWriteArrayList<Auction> ret = new CopyOnWriteArrayList<Auction>();

		Calendar now = Calendar.getInstance();
		for (Auction a : runningAuctions) {
			if (now.before(a.getDeadline())) {
				ret.add(a);
			}
		}
		
		return ret;
	}
}
